﻿using System;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Windows;
using System.Windows.Data;

namespace CoffeeInventory
{
    public partial class WarehousePage
    {
        public WarehousePage()
        {
            InitializeComponent();
        }

        private void ButtonClick(object sender, RoutedEventArgs e)
        {
            var addWindow = new AddWarehouseWindow();
            addWindow.ShowDialog();	
        }

        private void PageLoaded(object sender, RoutedEventArgs e)
        {
            var dBModelContainer = new DBModelContainer();
            //var warehouse = dBModelContainer.Counters.FirstOrDefault(
            //    counter => counter.Name.Equals("warehouse", StringComparison.InvariantCultureIgnoreCase));
            //if (warehouse != null)
            //{
            //    DataGrid1.ItemsSource = warehouse.Stocks.Select(stock => new { stock.Inventory.Name, stock.Quantity, stock.Inventory.Unit, stock.Inventory.Price });
            //}
            
            // Load data into Stocks. You can modify this code as needed.
            var stocksViewSource = ((CollectionViewSource)FindResource("stocksViewSource"));
            var stocksQuery = GetWarehouseStocks(dBModelContainer);
            stocksViewSource.Source = stocksQuery;
        }

        private EntityCollection<Stock> GetWarehouseStocks(DBModelContainer dBModelContainer)
        {
            // Auto generated code
            var firstOrDefault = dBModelContainer.Counters.FirstOrDefault(counter => counter.Name.Equals("warehouse", StringComparison.InvariantCultureIgnoreCase));
            if (firstOrDefault != null)
            {
                var stockQuery = firstOrDefault.Stocks;
                // Returns an ObjectQuery.
                return stockQuery;
            }
            return null;
        }
    }
}